$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL +'sys/pushsql/list',
        datatype: "json",
        colModel: [			
			{ label: 'rid', name: 'rid', index: 'rid', width: 50, key: true },
			{ label: '所属项目', name: 'project', index: 'project', width: 80 }, 			
			{ label: '所属数据库', name: 'module', index: 'module', width: 80 },
            { label: 'sql描述', name: 'describe', index: 'describe', width: 80 },
            { label: '预发布执行', name: 'pushPre', width: 50, formatter: function(value, options, row){
                return value === 1 ?
                    '<span class="label label-success">已执行</span>' :
                    '<span class="label label-danger pointer">未执行</span>';
            }},
			{ label: '发布时间', name: 'createTm', index: 'create_tm', width: 80 },
            { label: '发布人', name: 'createUser', index: 'create_user', width: 80 },
            { label: '预发布执行人', name: 'executor', index: 'executor', width: 80 },
            { label: '预发布版本标记', name: 'executeTag', index: 'execute_tag', width: 80 },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });

        }
    });
});


//引入vue后，需要添加vee-validata的js和语言包文件(目前只用中文)
VeeValidate.Validator.localize('zh_CN');
//js使用方式，指定语言、修改默认提示，添加自定义验证
VeeValidate.Validator.localize({
    zh_CN:{
        messages:{
            required:function (name) {
                return name+"不能为空！";
            }
        },//定义成我们需要的文字提示
        attributes:{
           createUser:'发布人',
           describe:'sql描述',
           pushSql:'需执行sql'
        },
    }
});
// VeeValidate.Validator.extend('phone',{ getMessage:function () { return "请输入正确手机号！"; },
// validate:function (value) { return /^1\d{10}$/.test(value); } });
Vue.use(VeeValidate);
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            module: null
		},
        rules: {

        },
		title: null,
        pushSql: {},
        dicList:{},
        projectDicList:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            this.errors.clear();
            $("#option_list").modal('show');
			vm.title = "新增";
			vm.pushSql = {ri:null,project:"宁家鲜生",module:"mall-trade"};
            // vm.getDicList();
            vm.getProjectDicList();
		},
		update: function (event) {
            this.errors.clear();
			var rid = getSelectedRow();
			if(rid == null){
				return ;
			}
            $("#option_list").modal('show');
            vm.title = "修改";
            
            vm.getInfo(rid);
            vm.getDicList();
            vm.getProjectDicList();
		},
		saveOrUpdate: function (event) {
            this.$validator.validateAll().then(function(result) {
                if (result) {
                    //成功操作
                    var url = vm.pushSql.rid == null ? baseURL +"sys/pushsql/save" : baseURL +"sys/pushsql/update";
                    $.ajax({
                        type: "POST",
                        url: url,
                        contentType: "application/json",
                        data: JSON.stringify(vm.pushSql),
                        success: function(r){
                            if(r.code === 0){
                                alert('操作成功', function(index){
                                    vm.reload();
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                }
            })

		},
        push_pre: function (event) {
			var rids = getSelectedRows();
			if(rids == null){
				return ;
			}
            $("#option_push_pre").modal('show');
            vm.title = "执行预发布sql";
            vm.pushSql = {ri:null,executor:"丹枫"};

		},
        pushPre: function (event) {
            var rids = getSelectedRows();
            if(rids == null){
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL +"sys/pushsql/push_pre?executor="+vm.pushSql.executor+"&executorTag="+vm.pushSql.executorTag,
                contentType: "application/json",
                data: JSON.stringify(rids),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getDicList: function(){
            $.get(baseURL + "sys/dic/select?code=DATABASE", function(r){
                vm.dicList = r.list;
            });
        },
        getProjectDicList: function(){
            $.get(baseURL + "sys/dic/select?code=PROJECT", function(r){
                vm.projectDicList = r.list;
            });
        },
		getInfo: function(rid){
			$.get(baseURL +"sys/pushsql/info/"+rid, function(r){
                vm.pushSql = r.pushSql;
            });
		},
		reload: function (event) {
            //$("#option_list").modal('hide');
            // vm.showList = true;
            this.errors.clear();
            $("#option_list").modal('hide');
            $("#option_push_pre").modal('hide');
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'module': vm.q.module,'pushPre': vm.q.pushPre},
                page:page
            }).trigger("reloadGrid");
		},
        close: function (event) {
            $("#option_list").modal('hide');
            $("#option_push_pre").modal('hide');
        },
        changeType: function (ele) {
            var optionTxt = $(ele.target).find("option:selected").text();
            var optionVal = ele.target.value;
        }
    },
    mounted: function () {
        this.getProjectDicList();
	    this.getDicList();
	}
});
